package com.kdtech.analyse.video;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;

/**
 * 白城电视网
 * @author Administrator
 * abc
 */
public class BctvAnalyse implements AnalyseNews{
	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://bctv.com.cn/article-[0-9]+-1.html"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta video = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt = urlMeta.getHtml();
		String url = urlMeta.getUrl();
		if(!isDetailPage(url)){
		}
		video.setUrl(url);
		String title = null;
		String desc = null;
		Long date = null;
		String dateStr = null;
		Document doc = Jsoup.parse(htmltxt);
		title = doc.select("div#ct.ct2 > div.mn >div.bm >div.h >h1.ph").text();

			date = DateUtils.matchDate(dateStr);
			if(date==null){
				date = DateUtils.matchDate(doc.select("div.h p.xg1").text());
			}
			video.setTitle(StringUtils.trimSpace(title));
			video.setContent(title); //无描述 标题作为描述
			video.setDate(date);
			String str = doc.select("div#ct.ct2 >div.mn >div.bm >div.h >p.xg1").text();
			String clicktemp = str.replace("|", "");
			int index = clicktemp.indexOf("查看:");
			int index2 = clicktemp.indexOf("评论:");
			String click = clicktemp.substring(index+3,index2).trim();
			String comment = clicktemp.substring(index2+3).trim();
			Integer clicknum = 0;
			Integer commentnum = 0;
			try {
				clicknum = Integer.valueOf(click);
			} catch (Exception e) {
				// TODO: handle exception
			}
			try {
				commentnum = Integer.valueOf(comment);
			} catch (Exception e) {
				// TODO: handle exception
			}
			video.setClickNum(clicknum);
			video.setCommentNum(commentnum);
			video.setUpdateUrl(url);
			return video;
	}
	

	
	public NewsMeta Update(NewsMeta meta) {
		if(meta!=null){
			String updateUrl = meta.getUpdateUrl();
			if(updateUrl!=null){
				UrlMeta responseToURL = CrawlHTML.responseToURL(updateUrl);
				if(responseToURL!=null){
					String html = responseToURL.getHtml();
					if(html!=null){
						Document doc = Jsoup.parse(html);
						String str = doc.select("div#ct.ct2 >div.mn >div.bm >div.h >p.xg1").text();
						String clicktemp = str.replace("|", "");
						int index = clicktemp.indexOf("查看:");
						int index2 = clicktemp.indexOf("评论:");
						String click = clicktemp.substring(index+3,index2).trim();
						String comment = clicktemp.substring(index2+3).trim();
						Integer clicknum = 0;
						Integer commentnum = 0;
						try {
							clicknum = Integer.valueOf(click);
						} catch (Exception e) {
							// TODO: handle exception
						}
						try {
							commentnum = Integer.valueOf(comment);
						} catch (Exception e) {
							// TODO: handle exception
						}
						meta.setClickNum(clicknum);
						meta.setCommentNum(commentnum);
						return meta;
					}
				}
			}
		}
		return null;
	}
	
}
